package com.zxl.live.screen.screen.ui.helper;

import android.content.Intent;

import com.zxl.live.screen.call.helper.ThemeHelper;
import com.zxl.live.screen.screen.constant.ICustomAction;
import com.zxl.live.screen.screen.constant.ISharedPreferences;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.screen.screen.ui.helper.window.WindowHelper;
import com.zxl.live.screen.screen.ui.receiver.ScreenReceiver;
import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.screen.screen.ui.widget.ScreenSurfaceView;
import com.zxl.live.screen.screen.ui.widget.panel.ScreenPanel;
import com.zxl.live.screen.screen.ui.widget.surface.FrameController;
import com.zxl.live.screen.ui.activity.KeepLiveActivity;
import com.zxl.process.sdk.DaemonClient;

/**
 * Created by Administrator on 2018/1/10.
 */

public class ScreenViewHelper implements ScreenReceiver.ScreenStatusListener {

    private ScreenService mContext;

    private ScreenSurfaceView mScreenSurfaceView;

    public ScreenViewHelper(ScreenService context) {
        this.mContext = context;
        ScreenReceiver.registerListener(this);
        reset();
        if (ScreenSetting.sIsNotificationEnabled) {
            context.startForeground(123, NotifHelper.getNotification(context));
        } else {
            DaemonClient.getInstance().setForgroundService(context, ScreenService.InnerService1.class);
        }
    }

    public void reset() {
        mScreenSurfaceView = new ScreenSurfaceView(mContext);
        if (ScreenSetting.sIsSwitchOpened) {
            FrameController.getInstance().setCurrentFream(ScreenPanel.getInstence(mContext));
            WindowHelper.getWindowHelper().addView(mScreenSurfaceView);
        }
    }

    public void onStartCommand(Intent intent) {
        if (ICustomAction.ACTION_REFRESH_SETTINGS.equals(intent.getAction())) {
            ScreenSetting.init();
            String key = intent.getStringExtra("data");
            if (ISharedPreferences.SETTINGS_SCREEN_SWITCH.equals(key)) {
                if (ScreenSetting.sIsSwitchOpened) {
                    FrameController.getInstance().setCurrentFream(ScreenPanel.getInstence(mContext));
                    WindowHelper.getWindowHelper().addView(mScreenSurfaceView);
                } else {
                    WindowHelper.getWindowHelper().removeView(mScreenSurfaceView);
                }
            } else if (ISharedPreferences.SETTINGS_SCREEN_TYPE.equals(key) || ISharedPreferences.SETTINGS_SCREEN_SIZE.equals(key)) {
                if (ScreenSetting.sIsSwitchOpened) {
                    ScreenPanel.getInstence(mContext).reset();
                    if (ScreenSetting.sIsNotificationEnabled) {
                        mContext.startForeground(123, NotifHelper.getNotification(mContext));
                    }
                }
            } else if (ISharedPreferences.SETTINGS_SCREEN_NOTIFICATION.equals(key)) {
                if (ScreenSetting.sIsNotificationEnabled) {
                    mContext.startForeground(123, NotifHelper.getNotification(mContext));
                } else {
                    mContext.stopForeground(true);
                    DaemonClient.getInstance().setForgroundService(mContext, ScreenService.InnerService1.class);
                }
            } else if (ISharedPreferences.SETTINGS_CALL_FLASH_INFO.equals(key) || ISharedPreferences.SETTINGS_CALL_FLASH_SWITCH.equals(key)) {
                ThemeHelper.refreshCallFlashInfo();
            }
        }
    }

    @Override
    public void onScreenOn() {
        if (ScreenSetting.sIsSwitchOpened) {
            mScreenSurfaceView.start();
        }
        KeepLiveActivity.stop();
    }

    @Override
    public void onScreenOff() {
        mScreenSurfaceView.stop();
//        ScreenLockActivity.lockScreen();
    }
}
